﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.FileUtil
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System.IO;
using System.Text;
using System.Threading.Tasks;

#nullable enable
namespace Commom.Util;

public static class FileUtil
{
  public static async Task<byte[]> ToBytesAsync(Stream stream)
  {
    stream.Seek(0L, SeekOrigin.Begin);
    byte[] buffer = new byte[stream.Length];
    int num = await stream.ReadAsync(buffer, 0, buffer.Length);
    byte[] bytesAsync = buffer;
    buffer = (byte[]) null;
    return bytesAsync;
  }

  public static byte[] ToBytes(Stream stream)
  {
    stream.Seek(0L, SeekOrigin.Begin);
    byte[] buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
    return buffer;
  }

  public static byte[] ToBytes(string data) => FileUtil.ToBytes(data, Encoding.UTF8);

  public static byte[] ToBytes(string data, Encoding encoding)
  {
    return string.IsNullOrWhiteSpace(data) ? new byte[0] : encoding.GetBytes(data);
  }

  public static byte[] Read(string filePath)
  {
    if (!File.Exists(filePath))
      return (byte[]) null;
    FileInfo fileInfo = new FileInfo(filePath);
    using (BinaryReader binaryReader = new BinaryReader((Stream) fileInfo.Open(FileMode.Open)))
      return binaryReader.ReadBytes((int) fileInfo.Length);
  }

  public static string ToString(
    Stream stream,
    Encoding encoding = null,
    int bufferSize = 2048 /*0x0800*/,
    bool isCloseStream = true)
  {
    if (stream == null)
      return string.Empty;
    if (encoding == null)
      encoding = Encoding.UTF8;
    if (!stream.CanRead)
      return string.Empty;
    using (StreamReader streamReader = new StreamReader(stream, encoding, bufferSize: bufferSize, leaveOpen: !isCloseStream))
    {
      if (stream.CanSeek)
        stream.Seek(0L, SeekOrigin.Begin);
      string end = streamReader.ReadToEnd();
      if (stream.CanSeek)
        stream.Seek(0L, SeekOrigin.Begin);
      return end;
    }
  }

  public static async Task<string> ToStringAsync(
    Stream stream,
    Encoding encoding = null,
    int bufferSize = 2048 /*0x0800*/,
    bool isCloseStream = true)
  {
    if (stream == null)
      return string.Empty;
    if (encoding == null)
      encoding = Encoding.UTF8;
    if (!stream.CanRead)
      return string.Empty;
    using (StreamReader reader = new StreamReader(stream, encoding, bufferSize: bufferSize, leaveOpen: !isCloseStream))
    {
      if (stream.CanSeek)
        stream.Seek(0L, SeekOrigin.Begin);
      string endAsync = await reader.ReadToEndAsync();
      if (stream.CanSeek)
        stream.Seek(0L, SeekOrigin.Begin);
      return endAsync;
    }
  }

  public static async Task<string> CopyToStringAsync(Stream stream, Encoding encoding = null)
  {
    if (stream == null)
      return string.Empty;
    if (encoding == null)
      encoding = Encoding.UTF8;
    if (!stream.CanRead)
      return string.Empty;
    using (MemoryStream memoryStream = new MemoryStream())
    {
      using (StreamReader reader = new StreamReader((Stream) memoryStream, encoding))
      {
        if (stream.CanSeek)
          stream.Seek(0L, SeekOrigin.Begin);
        stream.CopyTo((Stream) memoryStream);
        if (memoryStream.CanSeek)
          memoryStream.Seek(0L, SeekOrigin.Begin);
        string endAsync = await reader.ReadToEndAsync();
        if (stream.CanSeek)
          stream.Seek(0L, SeekOrigin.Begin);
        return endAsync;
      }
    }
  }
}
